import Toast from '@vant/weapp/toast/toast';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchValue: '',
        activeKey: 0,
        childrenKey: 0,
        list: [],
        secondary: [],
        productList: [],
        page: 1,
        children_type_id: null,
        showChildrenList: false,
        toView: '',
        scrollLeft: '',
        scrollViewWidth: '',
        resourceURL: '',

    },
    //  监听 Tabbar 点击事件
    onTabItemTap(item) {
        wx.requestSubscribeMessage({
			tmplIds: ["FopxpKaCJjLArs_b0C5hHJ9WxBQGkRaF7jr7w8SbT18"],
			success: res => {
				console.log("消息订阅", res);
			},
			fail: err => {
				console.error('[GM]：', err);
			}
		})
    },
    // 搜索商品
    onSearch(e) {
        setTimeout(() => {
            this.setData({
                searchValue: e.detail,
                page: 1,
                productList: []
            })
            this.getProductList()
        }, 500);
    },

    // 获取分类
    getCategoryList(key) {
       wx.http.get(wx.api.GetTypeList).then(res => {
             if (res.code == 1) {
                this.setData({
                    list: res.data,
                    secondary: res.data[key].children,
                    children_type_id: res.data[key].children[this.data.childrenKey].type_id
                })
                this.getProductList()
            }
            
        })
    },

    // 根据二级分类获取商品列表
    getProductList() {
        Toast.loading({
            message: '加载中...',
            duration: 0,
            forbidClick: true
        })
        wx.http.get(wx.api.productlist, {
            name: this.data.searchValue, //搜索商品名
            type_id: this.data.children_type_id, //二级分类id
            ssort: "asc", // 按销量的排序方式
            page: this.data.page, //页码
            limit: 6, //每页条数
            is_page:2,
        }).then((res) => {
            if (res.code == 1) {
                if (res.data.length > 0) {
                    this.setData({
                        productList: [...this.data.productList, ...res.data],
                        page: this.data.page + 1
                    })
                }
                if (res.data.length <= 0) {
                    Toast('没有更多了')
                    console.log(res.data);
                }
                setTimeout(() => {
                    Toast.clear()
                }, 500);
            }
        })
    },
    //切换左侧一级分类
    onChange(e) {
        this.setData({
            activeKey: e.detail,
            childrenKey: 0,
            secondary: [],
            productList: [],
            searchValue: '',
            page: 1
        })
            this.getCategoryList(this.data.activeKey)
       
    },

    //切换右侧二级分类
    changeChidren(e) {
        let index = e.currentTarget.dataset.index
        let hide = e.currentTarget.dataset.hide
        let offsetLeft = e.currentTarget.offsetLeft
        this.setData({
            productList: [],
            searchValue: '',
            childrenKey: index,
            children_type_id: this.data.secondary[index].type_id,
            showChildrenList: false,
            page: 1,
            toView: index,
            scrollLeft: hide == 1 ?
                offsetLeft - this.data.scrollViewWidth : offsetLeft - this.data.scrollViewWidth / 4
        })
        
        this.getProductList()
    },

    onShowChildrenList() {
        this.setData({
            showChildrenList: !this.data.showChildrenList
        })
    },

    pageTo(obj) {
        let e = obj.currentTarget.dataset.e
        wx.navigateTo({
            // url: `/packageC/product/index?id=${e.product_id}&s_id=${e.p_s_id}`,
            url: `/packageA/ProductDetails/index?id=${e.product_id}`,
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            resourceURL: `${wx.api.host}/`
        })
        this.getCategoryList(this.data.activeKey)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        wx.createSelectorQuery().select('.childrens_list').boundingClientRect((rect) => {
            this.data.scrollViewWidth = Math.round(rect.width)
        }).exec()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        this.getProductList()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
})